/*
 * PopupCustomButton.cpp
 *
 *  Created on: Mar 11, 2013
 *      Author: guillou
 */

#include <PopupLoggerUI.hpp>
#include <PopupCustomButton.hpp>

using namespace PopupUtils;

#define FIELD_SEPARATOR "&#&"

PopupCustomButton::PopupCustomButton(const QString & p_name,
                                     const QString & p_icon,
                                     const QString & p_media,
                                     const QString & p_text)
: m_name(p_name), m_icon(p_icon), m_media(p_media), m_text(p_text)
{
}

PopupCustomButton::PopupCustomButton(const PopupCustomButton & p_info)
: m_name(p_info.m_name), m_icon(p_info.m_icon),
  m_media(p_info.m_media), m_text(p_info.m_text)
{
}

void PopupCustomButton::setup(const QString & p_icon,
                   const QString & p_media,
                   const QString & p_text)
{
  m_icon  = p_icon;
  m_media = p_media;
  m_text  = p_text;
}

QString PopupCustomButton::toString() const
{
  return
      m_name + FIELD_SEPARATOR +
      m_icon + FIELD_SEPARATOR +
      m_media + FIELD_SEPARATOR +
      m_text;
}

PopupCustomButton *PopupCustomButton::fromString(const QString & p_string)
{
  QStringList s = p_string.split(FIELD_SEPARATOR);
  if (s.size() != 4)
  {
    error("Oops! Custom button string parsing failed :"
          "number of elements != 4");
    return 0;
  }
  else
  {
    return new PopupCustomButton(s.at(0), s.at(1), s.at(2), s.at(3));
  }
}


PopupCustomQtButton::PopupCustomQtButton(const PopupCustomButton & p_info)
: PopupCustomButton(p_info)
{
  setFocusPolicy(Qt::NoFocus);
  refresh();
}

void PopupCustomQtButton::setup(const PopupCustomButton & p_info)
{
  m_icon = p_info.iconPath();
  m_media = p_info.mediaUrl();
  m_text = p_info.msgText();
  refresh();
}


void PopupCustomQtButton::refresh()
{
  if (m_icon.length() > 0)
  {
    QIcon _icon;
    _icon.addFile(m_icon, QSize(32,32));
    setIcon(_icon);
    setText("");
  }
  else
  {
    setIcon(QIcon());
    setText(m_name);
  }
}
